package sh.wn36.vehiclemanage.controller.cjl_controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import sh.wn36.vehiclemanage.entity.*;
import sh.wn36.vehiclemanage.entity.entity_cjl.Violation;
import sh.wn36.vehiclemanage.entity.vo.ViolationQuery;
import sh.wn36.vehiclemanage.service.CarService;
import sh.wn36.vehiclemanage.service.DictionaryService;
import sh.wn36.vehiclemanage.service.DriverService;
import sh.wn36.vehiclemanage.service.service_cjl.PeccancyRecordService;
import sh.wn36.vehiclemanage.util.Result;

import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author xk
 * @since 2021-04-26
 */
@RestController
@CrossOrigin
@RequestMapping("/peccancy-record")
@Api("车辆违章记录模块接口")
public class PeccancyRecordController {

    @Autowired
    private PeccancyRecordService peccancyRecordService;

    @Autowired
    private CarService carService;

    @Autowired
    private DriverService driverService;

    @Autowired
    private DictionaryService dictionaryService;


    @ApiOperation(value = "按条件分页查询 违章记录")
    @PostMapping("/listViolations/{current}/{limit}")
    public Result listViolationsByPage(@PathVariable("current")Integer current, @PathVariable("limit")Integer limit, @RequestBody(required = false) ViolationQuery violationQuery){
        Page<Violation> teacherPage = new Page<>(current,limit);
        QueryWrapper<Violation> wrapper = new QueryWrapper<>();
        String carNo = violationQuery.getCarNo();
        if(violationQuery != null) {
            if (!StringUtils.isEmpty(carNo)) {
                wrapper.like("c.car_no", carNo);
            }
        }
        IPage<Violation> violationIPage = peccancyRecordService.listViolationByPage(teacherPage,wrapper);
        List<Violation> records = violationIPage.getRecords();
        long total = teacherPage.getTotal();
        return Result.OK().data("item",records).data("total",total);
    }


    @ApiOperation(value = "根据id删除违章表记录")
    @GetMapping("/removeViolation/{id}")
    public Result removeViolationById(@PathVariable("id") Integer id){
        boolean b = peccancyRecordService.removeById(id);
        if(b){
            return Result.OK();
        }else {
            return Result.Fail();
        }
    }


    @ApiOperation(value = "以违章记表为主表，查询所有从表")
    @GetMapping("/findAllNeedViolationCode")
    public Result findAllNeedViolationCode(){
        List<Car> carList = carService.list(null);
        List<Driver> driverList = driverService.list(null);
        List<Dictionary> dictionaryList = dictionaryService.list(null);
        return Result.OK().data("carList",carList).data("driverList",driverList).data("dictionaryList",dictionaryList);

    }

    @ApiOperation(value = "新增违章记录")
    @PostMapping("/saveViolation")
    public Result saveViolation(@RequestBody PeccancyRecord peccancyRecord){
        boolean save = peccancyRecordService.save(peccancyRecord);
        if(save){
            System.out.println("添加成功");
            return Result.OK();
        }else {
            System.out.println("添加失败");
            return Result.Fail();
        }
    }

    @ApiOperation(value = "根据id查询修改违章记录")
    @GetMapping("/findViolationById/{id}")
    public Result findViolation(@PathVariable("id")int id){
        PeccancyRecord violationByID = peccancyRecordService.findViolationByID(id);
        return Result.OK().data("violationByID",violationByID);
    }


    @ApiOperation(value = "根据封装对象的id修改违章记录表")
    @PostMapping("/updateViolation")
    public Result updateViolation(@RequestBody PeccancyRecord peccancyRecord){
        System.out.println(peccancyRecord);
        boolean update = peccancyRecordService.updateById(peccancyRecord);
        if(update){
            return Result.OK();
        }else {
            return Result.Fail();
        }
    }

    @GetMapping("/deleteSelectCheckBox/{ids}")
    public Result deleteSelectCheckBox(@PathVariable("ids") int[] ids){
        System.out.println("______________"+ids.toString());
        for(int i=0;i<ids.length;i++){
            peccancyRecordService.removeById(ids[i]);
        }
        return Result.OK();
    }

    @ApiOperation("报表接口")
    @GetMapping("/reportList")
    public Result reportList(){
        return Result.OK().data("reportList",peccancyRecordService.reportList());
    }
    @ApiOperation("报表按司机ID查询接口")
    @GetMapping("/queryReportByCarId/{carId}")
    public Result queryReportByCarId(@ApiParam("车辆ID") @PathVariable Integer carId){
        return Result.OK().data("reportList",peccancyRecordService.queryReportByCarId(carId));
    }
}

